/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.controller;

import com.minierp.bean.BbsArticleBean;
import com.minierp.bean.UserBean;
import com.minierp.dao.AccessoryDao;
import com.minierp.dao.BbsArticleDao;
import com.minierp.dao.BbsColumnDao;
import com.minierp.dao.IdGenerator;
import com.minierp.helper.EncCommonHelper;
import com.minierp.model.BbsArticle;
import com.minierp.model.BbsColumn;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

public class BbsArticleFormCtr extends SimpleFormController {
	private BbsColumnDao bbsColumnDao;
	private BbsArticleDao bbsArticleDao;
	private AccessoryDao accessoryDao;
	private IdGenerator idGenerator;

	protected Map referenceData(HttpServletRequest request) throws Exception {
		HashMap refData = new HashMap();
		String id = request.getParameter("id");
		if (id == null) {
			id = "";
		}

		refData.put("id", id);
		String parentId = request.getParameter("parent_id");
		if (parentId != null && !parentId.equals("") && !parentId.equals("0")) {
			refData.put(
					"parentTopic",
					this.bbsArticleDao.get(BbsArticle.class,
							Long.parseLong(parentId)));
		}

		String columnId = request.getParameter("column_id");
		refData.put("column", this.bbsColumnDao.get(BbsColumn.class,
				Long.parseLong(columnId)));
		String serverName = request.getServerName();
		String serverPort = String.valueOf(EncCommonHelper.getEnterpriseInfo(
				request).getUploadPort());
		refData.put("serverName", serverName);
		refData.put("serverPort", serverPort);
		return refData;
	}

	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		String id = request.getParameter("id");
		BbsArticleBean form = new BbsArticleBean();
		BbsArticle obj;
		if (id != null && !id.equals("")) {
			obj = (BbsArticle) this.bbsArticleDao.get(BbsArticle.class,
					Long.parseLong(id));
		} else {
			UserBean ub = EncCommonHelper.getCurrentUserAccount(request);
			obj = new BbsArticle();
			form.setTmpCode(ub.getUser().getId() + "_"
					+ System.currentTimeMillis());
			String parentId = request.getParameter("parent_id");
			if (parentId != null && !parentId.equals("")
					&& !parentId.equals("0")) {
				obj.setParentId(Long.parseLong(parentId));
			}

			String columnId = request.getParameter("column_id");
			obj.setColumnId(Long.parseLong(columnId));
		}

		form.setBbsArticle(obj);
		return form;
	}

	protected void onBind(HttpServletRequest request, Object command)
			throws Exception {
		BbsArticleBean form = (BbsArticleBean) command;
		if (form.getBbsArticle().getId() == 0L) {
			form.setAccessories(this.accessoryDao.getList("BBS",
					form.getTmpCode(), (String[]) null, 0L));
		} else {
			form.setAccessories(this.accessoryDao.getList("BBS", form
					.getBbsArticle().getId(), (String[]) null, 0L));
		}

	}

	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		String columnId = request.getParameter("column_id");
		ModelAndView mv = new ModelAndView(
				"redirect:/bbs/BbsTopicList.ihtm?column_id=" + columnId);
		String id = request.getParameter("id");
		UserBean ub = EncCommonHelper.getCurrentUserAccount(request);
		BbsArticleBean form = (BbsArticleBean) command;
		BbsArticle obj = form.getBbsArticle();
		String alertMsg = "";
		if (id != null && !id.equals("")) {
			this.bbsArticleDao.update(obj);
			alertMsg = "修改成功！";
		} else {
			obj.setId(this.idGenerator.getId(BbsArticle.class.getName()));
			String stafferName = "";
			long stafferId = 0L;
			if (ub.getStaffer() != null) {
				stafferName = ub.getStaffer().getStafferName();
				stafferId = ub.getStaffer().getId();
			}

			obj.setCreaterId(stafferId);
			obj.setCreaterName(stafferName);
			obj.setCreateTime(new Date());
			obj.setLastTime(obj.getCreateTime());
			if (obj.getParentId() == 0L
					&& this.accessoryDao.getList("BBS", form.getTmpCode(),
							(String[]) null, 0L).size() > 0) {
				obj.setHasAttach("Y");
			} else {
				obj.setHasAttach("N");
			}

			this.bbsArticleDao.save(obj);
			if (obj.getParentId() != 0L) {
				this.bbsArticleDao.update(obj.getParentId(), stafferId,
						stafferName, obj.getCreateTime());
				alertMsg = "回复发表成功！";
			} else {
				this.accessoryDao.update(form.getTmpCode(), obj.getId());
				this.bbsColumnDao.update(obj.getColumnId(), stafferId,
						stafferName, obj.getCreateTime(), obj.getId(),
						obj.getTitle());
				alertMsg = "主题发表成功！";
			}
		}

		mv.addObject("returnValue", String.valueOf(obj.getId()));
		EncCommonHelper.setAlertMessage(request, alertMsg);
		return mv;
	}

	public BbsArticleDao getBbsArticleDao() {
		return this.bbsArticleDao;
	}

	public void setBbsArticleDao(BbsArticleDao bbsArticleDao) {
		this.bbsArticleDao = bbsArticleDao;
	}

	public BbsColumnDao getBbsColumnDao() {
		return this.bbsColumnDao;
	}

	public void setBbsColumnDao(BbsColumnDao bbsColumnDao) {
		this.bbsColumnDao = bbsColumnDao;
	}

	public IdGenerator getIdGenerator() {
		return this.idGenerator;
	}

	public void setIdGenerator(IdGenerator idGenerator) {
		this.idGenerator = idGenerator;
	}

	public AccessoryDao getAccessoryDao() {
		return this.accessoryDao;
	}

	public void setAccessoryDao(AccessoryDao accessoryDao) {
		this.accessoryDao = accessoryDao;
	}
}